package graduation.claf.controller;

import graduation.claf.dao.BookMapper;
import graduation.claf.pojo.Book;
import graduation.claf.service.ifs.IBookService;
import graduation.claf.service.ifs.IOwnershipService;
import graduation.claf.tools.ResponseCode;
import graduation.claf.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static graduation.claf.tools.Tool.createUUID;

@RestController
@RequestMapping(value = "/portal")
public class BookController {

    @Autowired
    IBookService iBookService;
    @Autowired
    BookMapper bookMapper;

    @RequestMapping(value = "/book/searchByUserId.do")
    public ServerResponse FindBookByUser(String userId){
        return iBookService.SearchByUser(userId);
    }

    @RequestMapping(value = "/book/addNewBook.do")
    public ServerResponse AddNewBook(String bookName,int bookType,String bookPic,String creatorId){
        Book book = new Book(createUUID(),bookName,creatorId,bookType,1,bookPic,-1d,null,null);
        return iBookService.AddBook(book,creatorId);
    }

    @RequestMapping(value = "/book/inviteMember.do")
    public ServerResponse inviteMember(String senderId,String memberName,String bookId){
        return iBookService.inviteMember(senderId,memberName,bookId);
    }

    @RequestMapping(value = "/book/updateBudget.do")
    public ServerResponse updateBudget(String bookId,double money){
        if (bookMapper.updateBudget(bookId,money) == 0){
            return ServerResponse.createServerResponseByFail(
                    ResponseCode.SET_BUDGET_FAIL.getCode(),
                    ResponseCode.SET_BUDGET_FAIL.getMsg());
        }
        return ServerResponse.createServerResponseBySuccess();
    }


}
